package com.wolfpire.section.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.wolfpire.chapter.dao.ChapterDao;
import com.wolfpire.knowledge.dao.KnowledgeDao;
import com.wolfpire.section.dao.SectionDao;
import com.wolfpire.section.dao.StudyHistoryDao;
import com.wolfpire.subject.dao.SubjectDao;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.dao.UserDao;
import com.wolfpire.system.model.Chapter;
import com.wolfpire.system.model.DataDict;
import com.wolfpire.system.model.Knowledge;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.Section;
import com.wolfpire.system.model.StudyHistory;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.User;

@Repository("studyHistoryDao")
public class StudyHistoryDaoImpl extends BaseHibernateDao<StudyHistory, Long> implements StudyHistoryDao {

	public StudyHistory saveStudyHistory(long userId, long sectionId) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 根据用户ID和日历选择的时间查找学习记录
	 */
	public List<StudyHistory> findStudyHistory(long userId, String date) {
		StringBuffer querySql = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		querySql.append(" select sh.* from t_study_history sh");
		querySql.append(" where sh.user_id = :userId and  date(sh.record_time) = :date order by sh.record_time ");
		propertyNames.add("userId");
		propertyValues.add(userId);
		propertyNames.add("date");
		propertyValues.add(date);
		SQLQuery query = this.createSqlQuery(querySql.toString(), propertyNames, propertyValues);
		query.addEntity(StudyHistory.class);
		return query.list();
	}

	public int complempercent(long subjectId, long userId) {
		StringBuffer countSqlBuffer = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		countSqlBuffer.append(" select count(distinct sh.section_name) from t_study_history sh");
		if(userId != -1 ){          //防止用户不存在的情况
			countSqlBuffer.append(" where sh.user_id = :userId and sh.subject_id = :subjectId");
			propertyNames.add("userId");
			propertyValues.add(userId);
		}else
			countSqlBuffer.append(" where sh.subject_id = :subjectId");
		propertyNames.add("subjectId");
		propertyValues.add(subjectId);
		int totalCount = this.findSqlInt(countSqlBuffer.toString(), propertyNames, propertyValues);
		return totalCount;
	}

	public List<StudyHistory> findPinLunBySection(long sectionId,
			String flag) {
		StringBuffer querySql = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		querySql.append(" select sh.* from t_study_history sh");
		querySql.append(" where sh.section_id = :sectionId and sh.flag = :flag order by sh.record_time");
		propertyNames.add("sectionId");
		propertyValues.add(sectionId);
		propertyNames.add("flag");
		propertyValues.add(flag);
		SQLQuery query = this.createSqlQuery(querySql.toString(), propertyNames, propertyValues);
		query.addEntity(StudyHistory.class);
		return query.list();
	}

}
